<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class ColumnsController extends AdminBaseController{
	public function index(){	
        $list = M('columns')->field('id,pid,columns,addtime,status')->order('id')->select();
        $list = getTree($list,0,0,'——');
        $this->assign('list',$list);
        $this->display();
	}
	public function add(){
        $pid = I('get.pid/d',0);
        $list = M('columns')->field('id,pid,columns')->select();
        $list = getTree($list,0,0,"——");
        $this->assign('list',$list);
		$this->display();
	}
    public  function edit(){
        if(IS_GET) {
            $id = I("get.id/d", 0);
            if (!$id) {
                $this->error('非法操作');
            }
            $list = M('columns')->field('id,pid,columns')->select();
            $columnsInfo = M('columns')->field('id,pid,columns,seo_title,seo_keyword,seo_description,is_nav,status,sort,type')->where(['id' => $id])->find();
            $list = getTree($list,0,0,"——");
            $this->assign('list',$list);
            $this->assign('columnsInfo', $columnsInfo);
            $this->display('columns/add');
        }
    }
	public function save(){
		if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            $pid = I('post.pid/d',0);
            $status = I('post.status/d', 0);
            $columns = I('post.columns/s', '');
            if (!$columns) {
                $this->ajaxReturn(['status' => 1, 'msg' => '栏目名称不能为空！']);
            }
            $seo_title = I('post.seo_title/s', '');
            if(!$seo_title){
                $this->ajaxReturn(['status' => 1,'msg' => 'SEO标题不能为空！']);
            }
            $seo_keyword = I('post.seo_keyword/s', '');
            if(!$seo_keyword){
                $this->ajaxReturn(['status' => 1,'msg' => 'SEO关键词不能为空！']);
            }
            $seo_description = I('post.seo_description/s', '');
            if(!$seo_description){
                $this->ajaxReturn(['status' => 1,'msg' => 'SEO描述不能为空！']);
            }
            $type = I('post.type/d', 1);
            $is_nav = I('post.is_nav/d', 0);
            $sort = I('post.sort/d', 0);
            $data = [
            'id' => $id,
            'pid' => $pid,
            'status' => $status,
            'columns' => $columns,
            'seo_title' => $seo_title,
            'seo_keyword' => $seo_keyword,
            'seo_description' => $seo_description,
            'sort' => $sort,
            'type' => $type,
            'is_nav' => $is_nav
            ];
            $col = M('Columns');
            $isColumns = $col->where($data)->find();
            if($isColumns) {
                $this->ajaxReturn(['status' => 1, 'msg' => '栏目名称已存在！']);
            }
            $isIN_M = $col->add($data,[],true);
            if($isIN_M) {
                $this->ajaxReturn(['status' => 0, 'msg' => '成功！']);
            }
            $this->ajaxReturn(['status' => 1, 'msg' => '失败！']);
        }
	}
	
	public function changestatus(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id) || ($id == 1)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
            }
            $adminInfo = M('columns')->field('status')->where(['id'=>$id])->find();
            if(empty($adminInfo['status'])){
                M('columns')->where(['id' => $id])->setField('status',1);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已开启' , 'data' => 1]);
            }else{
                M('columns')->where(['id' => $id])->setField('status',0);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已禁用' , 'data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
        }
    }

    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id) || ($id == 1)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
            }
            $isDel = M('columns')->where(['id' => $id])->delete();
            if(!$isDel){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
        }
    }
}